package cn.stylefeng.guns.modular.lx.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.lx.entity.LxSpYsy;
import cn.stylefeng.guns.modular.lx.mapper.LxSpYsyMapper;
import cn.stylefeng.guns.modular.lx.model.params.LxSpYsyParam;
import cn.stylefeng.guns.modular.lx.model.result.LxSpYsyResult;
import  cn.stylefeng.guns.modular.lx.service.LxSpYsyService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 尹思莹
 * @since 2023-12-01
 */
@Service
public class LxSpYsyServiceImpl extends ServiceImpl<LxSpYsyMapper, LxSpYsy> implements LxSpYsyService {

    @Override
    public void add(LxSpYsyParam param){
        LxSpYsy entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(LxSpYsyParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(LxSpYsyParam param){
        LxSpYsy oldEntity = getOldEntity(param);
        LxSpYsy newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public LxSpYsyResult findBySpec(LxSpYsyParam param){
        return null;
    }

    @Override
    public List<LxSpYsyResult> findListBySpec(LxSpYsyParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(LxSpYsyParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public void update1(LxSpYsyParam lxSpYsyParam) {
        this.baseMapper.update1(lxSpYsyParam);

    }

    private Serializable getKey(LxSpYsyParam param){
        return param.getSpId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private LxSpYsy getOldEntity(LxSpYsyParam param) {
        return this.getById(getKey(param));
    }

    private LxSpYsy getEntity(LxSpYsyParam param) {
        LxSpYsy entity = new LxSpYsy();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
